<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：IJPushCache.class.php
 * @类功能: 极光推送类
 * @开发者: zc
 * @开发时间： 14-10-28
 * @版本：version 1.0
 */
namespace app\common\ext;

use app\common\enum\AppType;
use JPush\Model as M;
use JPush\JPushClient;
use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;

require_once 'JPush/push.php';

class IJPush {

    private static $app_map = [
        //客户端
        AppType::YH =>[
            'app_key'=>"f4d049a9a1894e8963fd02c3",
            'master_secret'=>"b8404841983b3442a0216976"
        ],
        //信贷经理端
        AppType::XD=>[
            'app_key'=>"f4d049a9a1894e8963fd02c3",
            'master_secret'=>"b8404841983b3442a0216976"
        ],
        //合作端
        AppType::HZ=>[
            'app_key'=>"f4d049a9a1894e8963fd02c3",
            'master_secret'=>"b8404841983b3442a0216976"
        ]
    ];

    /*
     * @功能 推送
     * @param string $title 标题
     * @param string $content 内容
     * @param string $alias 别名
     * @param string $param 参数
     * @开发者：cxl
     * @return bool
     */
    public static function Push($app_type,$title,$content,$alias=null,$param=array()) {
        if(!isset(self::$app_map[$app_type])){
            return false;
        }

        $app = self::$app_map[$app_type];

        //实例化对象
        $client = new JPushClient($app['app_key'], $app['master_secret']);

        //检测异常
        try {
            //判断发送方式
            if(empty($alias)){
                //发送推送
                $client->push()->setPlatform(M\all)->setAudience(M\all)
                    ->setNotification(M\notification($title, M\ios($content,'default','+1',true,$param), M\android($content,$title,1,$param)))
                    ->setOptions(M\options(null, 3600, null, true))
                    ->send();
            }else{
                //判断别名类型
                if(is_array($alias)){
                    $alias_send = $alias;
                }else{
                    $alias_send = array((string)$alias);
                }

                //发送推送
                $client->push()->setPlatform(M\all)->setAudience(M\audience(M\alias($alias_send)))
                    ->setNotification(M\notification($title, M\ios($content,'default','+1',true,$param), M\android($content,$title,1,$param)))
                    ->setOptions(M\options(null, 3600, null, true))
                    ->send();
            }

            //写日志
            if(!empty($alias)) {
                $message = self::log($app_type,$title, $content, $alias, $param);
                if ($message === false) {
                    return true;
                }
            }else{
                $message = self::log($app_type,$title, $content, null, $param);
                if ($message === false) {
                    return true;
                }
            }

            //返回结果
            return true;
        } catch (APIRequestException $e) {
            //写日志
            if(!empty($alias)) {
                $message = self::log($app_type,$title, $content, $alias, $param);
                if ($message === false) {
                    return "发送推送消息失败！";
                }
            }

            //返回异常信息
            return $e->getMessage();
        } catch (APIConnectionException $e) {
            //写日志
            if(!empty($alias)) {
                $message = self::log($app_type,$title, $content, $alias, $param);
                if ($message === false) {
                    return "发送推送消息失败！";
                }
            }

            //返回异常信息
            return $e->getMessage();
        }
    }

    /*
     * @功能 日志
     * @param string $app_type APP类型
     * @param string $title 标题
     * @param string $content 内容
     * @param string $alias 别名
     * @开发者：zc
     * @return bool
     */
    public static function log($app_type,$title,$content,$alias,$param){
        //判断参数类型
        if(is_array($param)){
            $param = json_encode($param);
        }

        //判断app类型
        if($app_type == AppType::YH){
            $push_data['user_id'] = $alias;
        }elseif($app_type == AppType::HZ){
            $push_data['cooperate_admin_id'] = $alias;
        }else{
            $push_data['agent_admin_id'] = $alias;
        }

        //整理数据
        $push_data['app_type'] = $app_type;
        $push_data['message_title'] = $title;
        $push_data['message_content'] = $content;
        $push_data['param'] = $param;
        $push_data['create_time'] = get_date_time();

        //写数据
        $message_add = IDb::getInstance("com_message")->setDbData($push_data)->add();
        if($message_add === false){
            return false;
        }

        //返回值
        return true;
    }
}